extends Node2D

@export var warn_duration: float = 1.0
@export var blink_hz: float = 4.0
@export var triangle_size: float = 20.0
@export var color: Color = Color(1, 0.2, 0.2, 1.0)

var _time: float = 0.0

func _ready() -> void:
	set_process(true)
	queue_redraw()

func _process(delta: float) -> void:
	_time += delta
	if _time >= warn_duration:
		_spawn_enemy()
		queue_free()
		return
	queue_redraw()

func _draw() -> void:
	# 闪烁透明度
	var alpha := 0.4 + 0.6 * 0.5 * (1.0 + sin(_time * TAU * blink_hz))
	var col := Color(color.r, color.g, color.b, clamp(alpha, 0.0, 1.0))
	# 画红色等边三角形（朝上）
	var h := triangle_size
	var w := triangle_size * 0.9
	var pts: PackedVector2Array = [
		Vector2(0, -h),
		Vector2(-w * 0.5, h * 0.5),
		Vector2(w * 0.5, h * 0.5),
	]
	draw_colored_polygon(pts, col)
	# 描边
	for i in range(3):
		draw_line(pts[i], pts[(i + 1) % 3], Color(col.r, col.g * 0.7, col.b * 0.7, 1.0), 2)
	# 感叹号
	var ex_col := Color(1,1,1,1)
	var ex_top := Vector2(0, -h * 0.5)
	var ex_bottom := Vector2(0, h * 0.1)
	draw_line(ex_top, ex_bottom, ex_col, 2)
	draw_circle(Vector2(0, h * 0.35), 2.0, ex_col)

func _spawn_enemy() -> void:
	# 生成基础敌人
	var enemy := BasicEnemy.new()
	# 确保存在碰撞体，便于 basic_enemy.gd 构建菱形与可视多边形
	var cs := CollisionShape2D.new()
	cs.shape = ConvexPolygonShape2D.new()
	enemy.add_child(cs)
	get_tree().current_scene.add_child(enemy)
	enemy.global_position = global_position

